package com.typing.controller;

import com.typing.entity.FileEntity;
import com.typing.utils.FileTools;
import com.typing.utils.Result;
import com.typing.utils.WebFaceDetect;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class FaceDetectController {

	@Value("${config.f_savePath}")
	private String savePath;

	@Value("${config.f_relativePath}")
	private String relativePath;

	@RequestMapping(value = "/upFace",method = RequestMethod.POST)
	@ResponseBody
	public Result upFace(@RequestParam("file")MultipartFile file){
		FileEntity fileEntity = new FileEntity();
		if (FileTools.hasFile(file.getOriginalFilename())){
			fileEntity.setSavePath(savePath);
			fileEntity.setRelativePath(relativePath);
		}
		FileEntity f_Entity = FileTools.uploadFile(file, fileEntity);
		String resultPath = f_Entity.getSavePath()+f_Entity.getRelativePath()+f_Entity.getFileName();
		if (resultPath!=null&&!"".equals(resultPath)){
			return new Result().ok(resultPath,"图片上传成功");
		}
		return new Result().upFileError();
	}

	@RequestMapping(value = "/resultFace",method =RequestMethod.GET)
	@ResponseBody
	public Result resultFace(@RequestParam("imagePath") String imagePath){
		if (imagePath!=null&&!"".equals(imagePath)){
			System.out.println("imagePath:"+imagePath);
			String result = WebFaceDetect.faceDetect(imagePath);
			return new Result().ok(result,"识别成功");
		}
		return new Result().systemError();
	}
}
